package com.zx.eval.api.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.R;
import com.zx.sys.cms.entity.CmsChannelEntity;
import com.zx.sys.cms.entity.CmsContentEntity;
import com.zx.sys.cms.service.CmsChannelService;
import com.zx.sys.cms.service.CmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("frontCmsApi")
@RestController
@RequestMapping("front/cms")
public class CmsApi {
    @Autowired
    private CmsChannelService cmsChannelService;
    @Autowired
    private CmsContentService cmsContentService;
    @Autowired
    private Environment env;

//    @Value("${company.address}")
//    private String companyAddress;
//    @Value("${company.tel}")
//    private String companyTel;
//    @Value("${company.coordinateX}")
//    private float companyCoordinateX;
//    @Value("${company.coordinateY}")
//    private float companyCoordinateY;


    /**
     * channel信息
     */
    @RequestMapping("/channelInfo")
    public R channelInfo(String type, Long id) {
        // 如果id位空则未类型，需要去配置文件取对应id
        if (id == null) {
            if ("banner".equals(type)) {
                id = 10L;// 根据配置文件调整
            } else if ("org".equals(type)) {
                id = 1L;// 根据配置文件调整 小哑铃 机构信息
            } else if("bottom".equals(type)){
                id = 5L;// 根据配置文件调整
            } else if("xlzx".equals(type)){
                id = 15L;// 心理咨询
            } else if("jyjc".equals(type)){
                id = 20L;// 基因检测
            } else if("org2".equals(type)){
                id = 25L;// 活动页面机构信息
            } else if("actives".equals(type)){
                id = 30L;// 活动栏目
            }

        }


        CmsChannelEntity info = cmsChannelService.getById(id);
        if (info.getIsDisplay() != 1){
            info = null;
        }
        return R.ok().put("info", info);
    }

    /**
     * channel列表
     */
    @RequestMapping("/channelList")
    public R channelList(Integer type) {
        QueryWrapper<CmsChannelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_content", 1);
        queryWrapper.eq("type", type);
        queryWrapper.eq("is_display", 1);
        List<CmsChannelEntity> list = cmsChannelService.list(queryWrapper);
        return R.ok().put("list", list);
    }

    /**
     * content列表
     */
    @RequestMapping("/contentTopList")
    public R contentTopList(Long id, Integer limit) {
        if(limit == null){
            limit = 4;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("channelId", id);
        params.put("limit", limit);
        List<CmsContentEntity> list = cmsContentService.selectTopList(params);
        return R.ok().put("list", list);
    }

    @RequestMapping("/contentsByChannel")
    public R getContentsByChannel(@RequestParam Map<String, Object> params){
        PageUtils page = cmsContentService.queryPageByChannel(params);
        System.out.println("channel.id" + params.get("channelId"));
        return R.ok().put("page", page);
    }
    /**
     * content内容
     */
    @RequestMapping("/contentInfo")
    public R contentInfo(Long id, Long channelId) {
        CmsContentEntity info = cmsContentService.getById(id);
        return R.ok().put("info", info);
    }

    @RequestMapping("/getOrgInfo")
    public R getOrgInfo(Integer id ) throws Exception{
        if(id != null && id.equals(1)){
            String companyAddress = new String(env.getProperty("company.name").getBytes("ISO-8859-1"), "UTF-8");
            String companyTel = new String(env.getProperty("company.tel"));
            String companyCoordinateX = new String(env.getProperty("company.coordinateX"));
            String companyCoordinateY = new String(env.getProperty("company.coordinateY"));

            return R.ok().put("companyAddress",companyAddress).put("companyTel",companyTel).put("companyCoordinateX",companyCoordinateX).put("companyCoordinateY",companyCoordinateY);

        }else {
            String companyTel = new String(env.getProperty("company2.tel"));
            String companyInfo = new String(env.getProperty("company2.info").getBytes("ISO-8859-1"), "UTF-8");
            String companyName = new String(env.getProperty("company2.name").getBytes("ISO-8859-1"), "UTF-8");
            String supportTelInfo = new String(env.getProperty("company2.tel.info").getBytes("ISO-8859-1"), "UTF-8");

            return R.ok().put("companyTel", companyTel).put("companyInfo", companyInfo).put("companyName", companyName).put("supportTelInfo", supportTelInfo);
        }
    }
}
